package net.huashitong.Project.util;


import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class WeixinHttpClientUtils {
	public static String doPost(String url, JSONObject json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json; charset=UTF-8");
        post.addHeader("Content-type","application/json; charset=utf-8");
        post.setHeader("Accept", "application/json");
        post.setEntity(new StringEntity(json.toString(), Charset.forName("UTF-8")));
        String result = null;
        try {
            HttpResponse res = client.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                HttpEntity entity = res.getEntity();
                result = EntityUtils.toString(res.getEntity());
            }
            System.out.println(111);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;  
    }
	public static void main(String[] args) throws UnsupportedEncodingException {
		Template tem=new Template();  
		tem.setTemplateId("Uz7YL53X3JGXSJSu2uxPYdlBHXG6wDAMpRD2fxuCXsc");  
		tem.setTopColor("#00DD00");  
		tem.setToUser("oTq72wNxHu_V6W0bRg69022dsrLI");  
		tem.setUrl("http://www.baidu.com");

		List<TemplateParam> paras=new ArrayList<TemplateParam>();  
		paras.add(new TemplateParam("first","恭喜！您有一笔新的奖励","#FF3333"));  
		paras.add(new TemplateParam("keyword1","¥20.00","#FF3333"));  
		paras.add(new TemplateParam("keyword2","发帖奖励","#FF3333"));  
		paras.add(new TemplateParam("Remark","感谢您对我们平台的支持，点击查看详细!","#AAAAAA"));  
		
		tem.setTemplateParamList(paras);  
		String access_token = "5_hlQ4SFJRIRlbUiNAQoBEarKCaoHWS3dFZy1NRzeJ_F_9l4lp17CRXdKELBDJh-bFJ635TA11aLOemkyV6CADZevBXTpHT6iEEfYcZDZmMDPcuzSJEPqd3hcSXEIHHRaAGARRB";		
		JSONObject jsonObject = JSONObject.parseObject(tem.toJSON());
		
		String res = WeixinHttpClientUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, jsonObject);
		System.out.println(res);
	}
}
